<!-- 采购统计表 -->
<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form inline size="mini" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="查看时间类型">
              <el-radio v-model="dataForm.timeType" label="1">按库存</el-radio>
              <el-radio v-model="dataForm.timeType" label="2" @change="selectTimeType">按成本</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="16" v-if="dataForm.timeType == '1'">
            <el-form-item label="出入库时间">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.createTimeStart"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
              <el-col class="line" :span="2" style="text-align: center;">至</el-col>
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.createTimeEnd"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="dataForm.timeType == '2'">
            <el-form-item label="结算时间">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.financeTimeStart"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
              <el-col class="line" :span="2" style="text-align: center;">至</el-col>
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.financeTimeEnd"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="成本显示方式" v-if="dataForm.timeType == 2">
          <el-radio v-model="dataForm.whichcost" label="1">基础信息含税 </el-radio>
          <el-radio v-model="dataForm.whichcost" label="2">基础信息不含税</el-radio>
          <el-radio v-model="dataForm.whichcost" label="3">单据含税</el-radio>
          <el-radio v-model="dataForm.whichcost" label="4">单据不含税</el-radio>
        </el-form-item>
        <el-row>
          <el-col :span="14">
            <el-form-item label="查看方式">
              <el-radio v-model="dataForm.selectType" label="1">按商品类型</el-radio>
              <el-radio v-model="dataForm.selectType" label="2">按制造商</el-radio>
              <el-radio v-model="dataForm.selectType" label="3">按品种</el-radio>
              <el-radio v-model="dataForm.selectType" label="4">按商品</el-radio>
              <el-radio v-model="dataForm.selectType" label="5">按单据</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="单据类型">
              <el-radio v-model="dataForm.logType" label="1">采购</el-radio>
              <el-radio v-model="dataForm.logType" label="2">委外</el-radio>
              <el-radio v-model="dataForm.logType" label="3">采购+委外</el-radio>
              <el-radio v-model="dataForm.logType" label="4">退货</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品类别">
              <el-select v-model="dataForm.ctgId" @change="changeCtgId" clearable>
                <el-option v-for="item in ctgOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制造商">
              <el-input v-model="dataForm.supplyName" @change="changeSupplyId">
                <el-button slot="append" @click="openSupplyDialog">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品品种" prop="brandName">
              <el-input v-model="dataForm.brandName" readonly="readonly">
                <el-button slot="append" @click="getBrandList">选择</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="商品代码">
              <el-input size="mini" v-model="dataForm.goodsCode" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品名称">
              <el-input size="mini" v-model="dataForm.goodsName" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单号">
              <el-input size="mini" v-model="dataForm.billId" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="运单号">
              <el-input size="mini" v-model="dataForm.transportBillId" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制单人">
              <el-input size="mini" v-model="dataForm.createUserName" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="审核人">
              <el-input size="mini" v-model="dataForm.auditUserName" style="width: 175px;"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否显示查询条件">
              <el-radio v-model="dataForm.queryShow" label="1">是</el-radio>
              <el-radio v-model="dataForm.queryShow" label="2">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="公司名称">
              <el-radio v-model="dataForm.isFlag" label="1">显示</el-radio>
              <el-radio v-model="dataForm.isFlag" label="2">隐藏</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" v-if="userInfo.lessDeptId == '1' || userInfo.lessDeptId == '21' || userInfo.lessDeptId == '23'">
            <el-form-item label="仓位名称"  >
              <el-input size="mini" type="textarea" v-model="dataForm.stockName" readonly>
              </el-input>
            </el-form-item>
            <el-form-item label-width="0">
              <el-button size="mini" @click="getStockList">选择</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-else>
            <el-form-item label="仓位名称"  >
              <el-input size="mini" type="textarea" v-model="dataForm.stockName" disabled>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">
            <el-link :href="printUrl" target="_blank" type="primary" :disabled="isDisabled">
              <el-button size="mini" type="primary" @click="getAllocationInfoList">查 询</el-button>
            </el-link>
          </el-form-item>
        </el-row>
      </el-form>
      <search-stock v-if="stockVisible" ref="stockInfo" @refreshData="getStockInfoList"></search-stock>
      <!-- 弹窗，查询制造商 -->
      <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
      <!-- 弹窗，查询商品品种 -->
      <select-brand v-if="brandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
    </basic-container>
  </div>
</template>

<script>
let params = "";
import { filterParams } from "@/util/tools";
import { getDateFormat } from "@/util/tools";
import { mapGetters } from "Vuex";
import { getUserInfo } from "@/api/admin/user"; //根据工号 获取用户信息
import searchStock from "@/components/business/stock-search";
import { getCtgList } from '@/api/eims/ctg'
import SearchSupply from '@/components/business/SearchSupply'
import SelectBrand from '../storage/SearchBrand'
import { getStockInfo } from '@/api/eims/stock'

export default {
  data() {
    return {
      printUrl: "",
      isDisabled: true,
      dataForm: {
        auditTimestart: '',
        auditTimeEnd: '',
        financeTimeStart: '',
        financeTimeEnd: '',
        whichcost: '1',
        timeType: '1',
        selectType: "1",
        logType: "1",
        goodsCode: "",
        goodsName: "",
        billId: '',
        transportBillId: '',
        createUserName: '',
        auditUserName: '',
        queryShow: '2',
        isFlag: '2',
        stockName: "",
        stockIds: [],
        supplyName: '',
        supplyId: '',
        brandName: '',
        brandId: ''
      },
      deptOptions: [],
      stockVisible: false,
      ctgOptions: [],
      supplyVisible: false,
      brandVisible: false,
    };
  },
  computed: {
    ...mapGetters(["userInfo", "bizData"])
  },
  components: {
    searchStock,
    SearchSupply,
    SelectBrand
  },
  created () {
    this.getGoodsCtgList();
    this.getStockName();
  },
  methods: {
      getStockName(){
        let deptId = this.userInfo.lessDeptId
        getStockInfo(deptId).then(result=>{
          this.dataForm.stockIds[0] = result.data.data[0].id;
          this.dataForm.stockName = result.data.data[0].name;
        })
      },
    getAllocationInfoList() {
      this.isDisabled = true
      if(this.dataForm.timeType == '1'){
        if(!this.dataForm.createTimeStart){
          this.$message.warning("请选择出入库查询日期范围");
          return
        }
        if(!this.dataForm.createTimeEnd){
          this.$message.warning("请选择出入库查询日期范围");
          return
        }
      }else{
        if(!this.dataForm.financeTimeStart){
          this.$message.warning("请选择结算日期范围");
          return
        }
        if(!this.dataForm.financeTimeEnd){
          this.$message.warning("请选择结算日期范围");
          return
        }
      }

      // if(this.dataForm.stockIds.length == 0){
      //   this.$message.warning("请选择仓库");
      //   return
      // }
      this.isDisabled = false
      if(this.dataForm.timeType != '2'){
        this.dataForm.financeTimeStart =''
        this.dataForm.financeTimeEnd = ''
      }
      if(this.dataForm.timeType != '1'){
        this.dataForm.whichcost = 0
        this.dataForm.createTimeStart = ''
        this.dataForm.createTimeEnd = ''
      }
      if(this.dataForm.selectType =='1'){
        params = "/cghz/storage_cghz_goods_type.cpt";
      }else if(this.dataForm.selectType =='2'){
        params = "/cghz/storage_cghz_supply.cpt";
      }else if(this.dataForm.selectType =='3'){
        params = "/cghz/storage_cghz_brand.cpt";
      }else if(this.dataForm.selectType =='4'){
        params = "/cghz/storage_cghz_goods.cpt";
      }else if(this.dataForm.selectType =='5'){
        params = "/cghz/storage_cghz_bill.cpt";
      }
      let data = filterParams(this.dataForm);
      let parameter = "";
      for (let key in data) {
        parameter += "&" + key + "=" + this.dataForm[key];
      }
      this.printUrl = this.bizData.printUrl + params + parameter;

    },
    //根据工号获取员工真实名称
    getRealName(workNumber) {
      if (workNumber == "") {
        this.dataForm.createUserName = "";
        this.dataForm.createUser = "";
      } else {
        getUserInfo(workNumber).then(result => {
          if (result.data.data) {
            this.dataForm.createUserName = result.data.data.realName;
            this.dataForm.createUser = result.data.data.id;
          } else {
            this.$message.warning("请输入正确的员工工号");
          }
        });
      }
    },
    getStockList() {
      this.stockVisible = true;
      let data = [];
      if (this.dataForm.stockIds) {
        data = this.dataForm.stockIds;
      }
      this.$nextTick(() => {
        this.$refs.stockInfo.init(data);
      });
    },
    getStockInfoList(data) {
      let name = "";
      this.dataForm.stockIds = []
      data.forEach(result => {
        this.dataForm.stockIds.push(result.id);
        name = name ? name + "," + result.name : result.name;
      });
      this.dataForm.stockName = name;
    },
    //  获取商品类别
    getGoodsCtgList() {
      getCtgList().then(result => {
        this.ctgOptions = result.data.data
      })
    },
    changeCtgId(){
      this.dataForm.supplyId = ''
      this.dataForm.supplyName = ''
      this.dataForm.brandId = ''
      this.dataForm.brandName= ''
    },
    //  打开选择制造商弹窗
    openSupplyDialog() {
      if (!this.dataForm.ctgId) {
        this.$message.warning('请选择商品类别')
        return false
      }
      let data = {
        ctgId: this.dataForm.ctgId
      }
      this.supplyVisible = true
      this.$nextTick(() => {
        this.$refs.supplyDialog.init(data)
      })
    },
    getSupplyData(data){
      this.$set(this.dataForm,'supplyId',data.id)
      this.$set(this.dataForm,'supplyName',data.name)
    },
    changeSupplyId(){
      this.dataForm.brandId = ''
      this.dataForm.brandName= ''
    },
    // 获取商品品种信息
    getBrandList() {
      if (!this.dataForm.ctgId) {
        this.$message.warning('请选择商品类别')
        return false
      }
      if (!this.dataForm.supplyId) {
        this.$message.warning('请选择制造商')
        return false
      }
      let data = {
        ctgId: this.dataForm.ctgId,
        supplyId: this.dataForm.supplyId
      }
      this.brandVisible = true
      this.$nextTick(() => {
        this.$refs.selectBrand.init(data)
      })
    },
    //  更新表单品种信息
    updateFormBrand(data) {
      this.dataForm.brandId = data.id
      this.dataForm.brandName = data.name
      this.dataForm.brandCode = data.code
      this.dataForm.name = data.name
      this.dataForm.unitType = data.unitType
    },
    selectTimeType(){
      this.dataForm.whichcost ='1'
    }
  }
};
</script>

<style lang="scss" scoped>
.row-bg {
  background-color: #cadee8;
  // padding-left: 15px;
  padding-right: 15px;
  padding-top: 5px;
  margin-bottom: 2px;
}
</style>
